package com.book.servlet;
/**
 * 包含对我的留言增.删.改 方法
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.bean.Messagess;
import com.book.bean.Reply;
import com.book.bean.Users;
import com.book.biz.MessageBiz;
import com.book.biz.UserBiz;
import com.book.biz.impl.MessageBizImpl;
import com.book.biz.impl.UserBizImpl;

public class AllMessageServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		String type = request.getParameter("type");
		//新增我的留言
		if("add".equals(type)){
			String uname = (String)request.getSession().getAttribute("uname");
			if(uname==null){
				response.sendRedirect("login_user.jsp");
			}else{
				
				String title = request.getParameter("title");
				String content = request.getParameter("content");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				df.format(new Date());
				
				Messagess msg = new Messagess();
				msg.setUser(uname);
				msg.setTitle(title);
				msg.setContent(content);
				msg.setMtime(df.format(new Date()));
				
				MessageBiz msgBiz = new MessageBizImpl();
				boolean flag = msgBiz.addMessage(msg);
				if(flag){
					out.print("<script>alert('OK');</script>");
					response.sendRedirect("SelectMessage.do?type=my");
				}else{
					request.getRequestDispatcher("error.jsp").forward(request, response);
				}
			}
		}
		//删除一条留言
		if("delete".equals(type)){
			
			int mid = Integer.parseInt(request.getParameter("mid"));
			MessageBiz msgBiz = new MessageBizImpl();
			boolean flag = msgBiz.deleteMessage(mid);
			if(flag){
				out.print("<script>alert('OK');</script>");
				response.sendRedirect("SelectMessage.do?type=my");
			}else{
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}	
		//获取一条留言
		if("getOne".equals(type)){
			int mid = Integer.parseInt(request.getParameter("mid"));
			MessageBiz msgBiz = new MessageBizImpl();
			Messagess msg = msgBiz.getOneMesssage(mid);
			List<Reply> replyList = new ArrayList<Reply>();
			replyList = msgBiz.listReply(mid);
			request.setAttribute("replyList", replyList);
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("message_one.jsp").forward(request, response);
		}		
		//获取一条留言
		if("one".equals(type)){
			int mid = Integer.parseInt(request.getParameter("mid"));
			MessageBiz msgBiz = new MessageBizImpl();
			Messagess msg = msgBiz.listMsgById(mid);
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("message_modify.jsp").forward(request, response);
		}		
		//修改一条留言
		if("modify".equals(type)){
			int mid = Integer.parseInt(request.getParameter("mid"));
			MessageBiz msgBiz = new MessageBizImpl();
			boolean flag = msgBiz.modifyMessage(mid);
			if(flag){
				System.out.println("留言修改成功了");
			}else{
				System.out.println("修改失败了fail......");
			}
			//新增
			String uname = (String)request.getSession().getAttribute("uname");
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			df.format(new Date());
			
			Messagess msg = new Messagess();
			msg.setUser(uname);
			msg.setTitle(title);
			msg.setContent(content);
			msg.setMtime(df.format(new Date()));
			boolean flag1 = msgBiz.addMessage(msg);
			if(flag1){
				out.print("<script>alert('OK');</script>");
				response.sendRedirect("SelectMessage.do?type=my");
			}else{
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}	
		//新增回复的留言
		if("addReply".equals(type)){
			String uname = (String)request.getSession().getAttribute("uname");
			if(uname==null){
				response.sendRedirect("login_user.jsp");
			}else{
				
				int mid = Integer.parseInt(request.getParameter("mid"));
				String content = request.getParameter("content");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
				df.format(new Date());
				int num=(int)(Math.random()*10);
				String lastTwo = String.valueOf(num);
				
				Reply reply = new Reply();
				reply.setReply_id(df1.format(new Date()).concat(lastTwo));
				reply.setReply_content(content);
				Messagess msg = new Messagess();
				msg.setMid(mid);
				reply.setMessagess(msg);
				reply.setReply_time(df.format(new Date()));
				
				MessageBiz msgBiz = new MessageBizImpl();
				boolean flag = msgBiz.addReply(reply);
				if(flag){
//					out.print("<script>alert('感谢您的回复');</script>");
					response.sendRedirect("AllMessage.do?type=getOne&&mid="+mid);
				}else{
					request.getRequestDispatcher("error.jsp").forward(request, response);
				}
			}
		}
		
	}

}
